package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

//        //当查询条件不为空时且不为“”，设置页码=1
//        if (queryPageBean.getQueryString() != null) {
//            String strip = queryPageBean.getQueryString().strip();
//            queryPageBean.setQueryString(strip);
//            if (!"".equals(strip)) {
//                queryPageBean.setCurrentPage(1);
//            }
//        }
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Setmeal> setmeals = setmealDao.findPage(queryPageBean);
        Page<Setmeal> page = (Page<Setmeal>) setmeals;
        if (page != null) {
            return new PageResult(page.getTotal(), page.getResult());
        }

        return null;
    }

    //根据id查询
    @Override
    public Setmeal findById(Integer id) {
        if (id != null && id > 0) {
            return setmealDao.findById(id);
        }
        return null;
    }

    //新增set
    @Transactional
    @Override
    public Integer addSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {
        //插入setmeal
        setmealDao.addSetmeal(setmeal);
        //新增setmeal_group
        if (setmeal.getId() != null && setmeal.getId() > 0) {
            setmealDao.insertSetIdAndGroupIds(setmeal.getId(), checkgroupIds);
            return setmeal.getId();
        }
        return null;
    }
}
